<template>
  <div class="cus-indexlist">
    <ul class="cus-indexlist-content" ref="content" :style="{ 'height': currentHeight + 'px'}">
      <slot></slot>
    </ul>

    <div class="cus-indexlist-nav" @touchstart="handleTouchStart" ref="nav">
      <ul class="cus-indexlist-navlist">
        <li class="cus-indexlist-navitem" v-for="section in sections" :key="section.index">{{ section.index }}</li>
      </ul>
    </div>

    <div class="cus-indexlist-indicator" v-if="showIndicator" v-show="moving">{{ currentIndicator }}</div>
  </div>
</template>

<script>
export default {
  name: 'index-list',
  props: {
    height: Number,
    showIndicator: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      sections: [],
      // navWidth: 0,
      indicatorTime: null,
      moving: false,
      firstSection: null,
      currentIndicator: '',
      currentHeight: this.height,
      navOffsetX: 0
    }
  },
  watch: {
    sections () {
      this.init()
    },
    height (val) {
      if (val) {
        this.currentHeight = val
      }
    }
  },
  methods: {
    init () {
      // this.$nextTick(() => {
      //   this.navWidth = this.$refs.nav.clientWidth
      // })
      let listItems = this.$refs.content.getElementsByTagName('li')
      if (listItems.length > 0) {
        this.firstSection = listItems[0]
      }
    },
    handleTouchStart (e) {
      if (e.target.tagName !== 'LI') {
        return
      }
      this.navOffsetX = e.changedTouches[0].clientX
      this.scrollList(e.changedTouches[0].clientY)
      if (this.indicatorTime) {
        clearTimeout(this.indicatorTime)
      }
      this.moving = true
      window.addEventListener('touchmove', this.handleTouchMove)
      window.addEventListener('touchend', this.handleTouchEnd)
    },
    handleTouchMove (e) {
      e.preventDefault()
      this.scrollList(e.changedTouches[0].clientY)
    },
    handleTouchEnd () {
      this.indicatorTime = setTimeout(() => {
        this.moving = false
        this.currentIndicator = ''
      }, 500)
      window.removeEventListener('touchmove', this.handleTouchMove)
      window.removeEventListener('touchend', this.handleTouchEnd)
    },
    scrollList (y) {
      let currentItem = document.elementFromPoint(this.navOffsetX, y)
      if (!currentItem || !currentItem.classList.contains('cus-indexlist-navitem')) {
        return
      }
      this.currentIndicator = currentItem.innerText
      let targets = this.sections.filter(section => section.index === currentItem.innerText)
      let targetDOM
      if (targets.length > 0) {
        targetDOM = targets[0].$el
        this.$refs.content.scrollTop = targetDOM.getBoundingClientRect().top - this.firstSection.getBoundingClientRect().top
      }
    }
  },
  mounted () {
    if (!this.currentHeight) {
      window.scrollTo(0, 0)
      requestAnimationFrame(() => {
        this.currentHeight = document.documentElement.clientHeight - this.$refs.content.getBoundingClientRect().top
      })
    }
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.cus-indexlist {
  width: 100%;
  position: relative;
  overflow: hidden;

  .cus-indexlist-content {
    margin: 0;
    padding: 0;
    overflow: auto;
  }

  .cus-indexlist-nav {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    margin: 0;
    text-align: center;
    max-height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .cus-indexlist-navlist {
    padding: 0;
    margin: 0;
    list-style: none;
    max-height: 100%;
    display: flex;
    flex-direction: column;
  }

  .cus-indexlist-navitem {
    padding: .053333rem .213333rem;
    font-size: .32rem;
    user-select: none;
    color: #5a5a5a;
    -webkit-touch-callout: none;
  }

  .cus-indexlist-indicator {
    position: absolute;
    width: 50px;
    height: 50px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    line-height: 50px;
    background-color: rgba(0, 0, 0, .7);
    border-radius: 5px;
    color: #fff;
    font-size: 22px;
  }
}
</style>
